package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.BuyCartBean;
import com.project.bean.BuyDetailBean;
import com.project.bean.BuyOrderBean;
import com.project.bean.ProductBean;
import com.project.mapper.IBuyCartMapper;
import com.project.mapper.IBuyDetailMapper;
import com.project.mapper.IBuyOrderMapper;
import com.project.mapper.IProductMapper;
import com.project.service.IBuyCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BuyCartServiceImpl implements IBuyCartService {
    @Autowired
    private IBuyCartMapper buyCartMapper;
    @Autowired
    private IBuyOrderMapper buyOrderMapper;
    @Autowired
    private IBuyDetailMapper buyDetailMapper;
    @Autowired
    private IProductMapper productMapper;

    @Override
    public void addBuyCart(ProductBean product, Integer userId, Integer buyNum) {
        //通过库存id找到该商品
        ProductBean productBean = productMapper.selectById(product.getProductId());
        //判断该商品是否存在
        if (productBean != null) {//如果存在，就在采购车的原有商品上数量加1
            QueryWrapper<BuyCartBean> qw = new QueryWrapper<>();
            qw.eq("fk_productId", product.getProductId());
            qw.eq("fk_userId", userId);
            BuyCartBean buyCart = buyCartMapper.selectOne(qw);
            if (buyCart != null) {
                buyCart.setBuyNum(buyCart.getBuyNum() + buyNum);
                buyCartMapper.updateById(buyCart);
            } else {
                BuyCartBean buyCartBean = new BuyCartBean(productBean.getNewBuyPrice(), buyNum, product.getProductId(), userId);
                buyCartMapper.insert(buyCartBean);
            }

        } else {//如果不存在，就在库存里记录一条商品数量为0的数据，然后添加商品在采购车里，数量为1
            ProductBean productBean1 = new ProductBean(product.getProductId()
                    , product.getpName(), product.getBrand(), product.getType(), product.getImgPath());
            productBean1.setStockNum(0);
            productBean1.setSellPrice(0);
            productMapper.insert(productBean1);
            BuyCartBean buyCart = new BuyCartBean(productBean1.getNewBuyPrice(), buyNum, productBean1.getProductId(), userId);
            buyCartMapper.insert(buyCart);
        }

    }

    @Override
    public List<BuyCartBean> findBuyCartByUserId(Integer userId) {
        return buyCartMapper.findBuyCartByUserId(userId);
    }

    @Override
    public void updateBuyPrice(Integer buyCartId, Integer newBuyPrice) {
        BuyCartBean buyCart = buyCartMapper.selectById(buyCartId);
        buyCart.setBuyPrice(newBuyPrice);
        buyCartMapper.updateById(buyCart);
    }

    @Override
    public void updateBuyNum(Integer buyCartId, Integer newBuyNum) {
        BuyCartBean buyCart = buyCartMapper.selectById(buyCartId);
        buyCart.setBuyNum(newBuyNum);
        buyCartMapper.updateById(buyCart);
    }

    @Override
    public void delBuyCart(Integer buyCartId) {
        buyCartMapper.deleteById(buyCartId);
    }

    @Override
    public void in(Integer userId, String sName) {
        BuyOrderBean order = new BuyOrderBean(sName, userId);
        buyOrderMapper.insert(order);
        List<BuyCartBean> buyCartList = buyCartMapper.findBuyCartByUserId(userId);
        buyDetailMapper.addBuyDetail(order.getBuyOrderId(), buyCartList);
        for (int i = 0; i < buyCartList.size(); i++) {
            BuyCartBean buyCart = buyCartList.get(i);
            ProductBean product = productMapper.selectById(buyCart.getProductId());
            product.setStockNum(product.getStockNum() + buyCart.getBuyNum());
            product.setNewBuyPrice(buyCart.getBuyPrice());
            productMapper.updateById(product);
        }
        QueryWrapper<BuyCartBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId", userId);
        buyCartMapper.delete(qw);
    }
}
